package de.tabuma.sso.group;


import java.lang.reflect.*;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.google.appengine.api.users.*;

import de.tabuma.lib.*;
import de.tabuma.lib.contentmanagement.entity.*;

import de.tabuma.customer.entity.*;
import de.tabuma.customer.logik.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1.2.1 $
 * @since $Date: 2010/01/13 10:23:10 $
 */
public class GroupController extends tabumacontroller {

    private static final Logger log = Logger.getLogger(GroupController.class.getName());

    public GroupController(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        try {

            UserService userService = UserServiceFactory.getUserService();
            com.google.appengine.api.users.User user = userService.getCurrentUser();


            if(request.getParameter(de.tabuma.sso.constants.GROUP_USER_SIGN)!=null) {
                de.tabuma.sso.usergroup.UserGroupController usergroup = new de.tabuma.sso.usergroup.UserGroupController(this.getServlet(), this.getParameter());
                usergroup.navigate(context, request, response, out);
                return;
            }

            if(request.getParameter(de.tabuma.sso.constants.GROUP_PRIVILEG_SIGN)!=null) {
                de.tabuma.sso.groupprivileg.GroupPrivilegController groupprivileg = new de.tabuma.sso.groupprivileg.GroupPrivilegController(this.getServlet(), this.getParameter());
                groupprivileg.navigate(context, request, response, out);
                return;
            }
            
            String name = "groupname";
            String customer = "customer";
            String adminProject = "project administration";
            String adminGroup = "group administration";
            String adminCustomer = "customer administration";
            String adminUser = "user administration";
            String adminNoLicenseNeeded = "no license needed (nln's)";
            String adminAbleToCreateGroupWithNoLicenseNeeded = "able to create nln groups";
            String adminAll = "Admin all";

            String entityName = "group";

            String user1 = "group member";
            String privileges = "privileges";

            String pageHead = "Group Management";

            out.println(view.getContentHeader(pageHead));

            Content content = new Content();
            
            Vector<Field> editableFields = new Vector<Field>();
            editableFields.add(de.tabuma.sso.group.Group.class.getDeclaredField("name"));
            editableFields.add(de.tabuma.sso.group.Group.class.getDeclaredField("customerId"));
            editableFields.add(de.tabuma.sso.group.Group.class.getDeclaredField("adminProject"));
            editableFields.add(de.tabuma.sso.group.Group.class.getDeclaredField("adminGroup"));
            editableFields.add(de.tabuma.sso.group.Group.class.getDeclaredField("adminCustomer"));
            editableFields.add(de.tabuma.sso.group.Group.class.getDeclaredField("adminUser"));
            if(userService.isUserAdmin()) {
                editableFields.add(de.tabuma.sso.group.Group.class.getDeclaredField("noLicenseNeeded"));
                editableFields.add(de.tabuma.sso.group.Group.class.getDeclaredField("ableToCreateGroupWithNoLicenseNeeded"));
                editableFields.add(de.tabuma.sso.group.Group.class.getDeclaredField("adminAll"));
            }
            content.FormContent.setEditableFields(editableFields);

            Vector<Integer> editableTypes = new Vector<Integer>();
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_STRING));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_LIST));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_BOOLEAN));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_BOOLEAN));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_BOOLEAN));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_BOOLEAN));
            if(userService.isUserAdmin()) {
                editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_BOOLEAN));
                editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_BOOLEAN));
                editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_BOOLEAN));
            }
            content.FormContent.setEditableFieldTypes(editableTypes);
            
            content.FormContent.setEntityClass(de.tabuma.sso.group.Group.class);
            content.FormContent.setEntityName(entityName);
            content.FormContent.setFacadeObject(new GroupFacade());

            content.ListContent.setListParameterValue(user.getEmail());

            content.GeneralContent.setParamName(de.tabuma.sso.constants.GROUP_PARAM);

            Vector<Field> listableFields = new Vector<Field>();
            listableFields.add(de.tabuma.sso.group.Group.class.getDeclaredField("name"));
            content.ListContent.setListableFields(listableFields);

            Hashtable<String,Hashtable<String,String>> additionalListFields = new Hashtable<String,Hashtable<String,String>>();
            Hashtable<String,String> parameterHash = (Hashtable<String,String>) this.getParameter().clone();
            parameterHash.put(de.tabuma.sso.constants.GROUP_USER_SIGN, "true");
            additionalListFields.put(user1, parameterHash);

            Hashtable<String,String> parameterHash1 = (Hashtable<String,String>) this.getParameter().clone();
            parameterHash1.put(de.tabuma.sso.constants.GROUP_PRIVILEG_SIGN, "true");
            additionalListFields.put(privileges, parameterHash1);
            content.ListContent.setAdditionalListFields(additionalListFields);

            content.GeneralContent.setServlet(this.getServlet());

            content.GeneralContent.setParameter(this.getParameter());

            Hashtable<Field, String> displayFields = new Hashtable<Field, String>();
            displayFields.put(de.tabuma.sso.group.Group.class.getDeclaredField("name"), name);
            displayFields.put(de.tabuma.sso.group.Group.class.getDeclaredField("customerId"), customer);
            displayFields.put(de.tabuma.sso.group.Group.class.getDeclaredField("adminProject"), adminProject);
            displayFields.put(de.tabuma.sso.group.Group.class.getDeclaredField("adminGroup"), adminGroup);
            displayFields.put(de.tabuma.sso.group.Group.class.getDeclaredField("adminCustomer"), adminCustomer);
            displayFields.put(de.tabuma.sso.group.Group.class.getDeclaredField("adminUser"), adminUser);
            if(userService.isUserAdmin()) {
                displayFields.put(de.tabuma.sso.group.Group.class.getDeclaredField("noLicenseNeeded"), adminNoLicenseNeeded);
                displayFields.put(de.tabuma.sso.group.Group.class.getDeclaredField("ableToCreateGroupWithNoLicenseNeeded"), adminAbleToCreateGroupWithNoLicenseNeeded);
                displayFields.put(de.tabuma.sso.group.Group.class.getDeclaredField("adminAll"), adminAll);
            }
            content.FormContent.setDisplayedName(displayFields);

            Hashtable<Field, List> listParameterContent = new Hashtable<Field, List>();
            List<Customer> customerList = CustomerFacade.getCustomer(user.getEmail());
            listParameterContent.put(de.tabuma.sso.group.Group.class.getDeclaredField("customerId"), customerList);
            content.ListContent.setListParameterContent(listParameterContent);

            Hashtable<Field,List<Field>> listParameterContentDisplayedFields = new Hashtable<Field, List<Field>>();
            List<Field> displayFieldList = new ArrayList<Field>();
            displayFieldList.add(de.tabuma.customer.entity.Customer.class.getDeclaredField("name"));
            listParameterContentDisplayedFields.put(de.tabuma.sso.group.Group.class.getDeclaredField("customerId"), displayFieldList);
            content.ListContent.setListParameterDisplayedField(listParameterContentDisplayedFields);

            content.GeneralContent.setUser(user.getEmail());

            out.println(content.navigate(request));
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
    }
}